/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.TaskPlanPageQueryDTO;
import org.springblade.modules.admin.entity.MaintainTemplate;
import org.springblade.modules.admin.entity.MaintainTemplateSceneRelation;
import org.springblade.modules.admin.service.MaintainTemplateService;
import org.springblade.modules.admin.vo.TemplateDetailVO;
import org.springblade.modules.admin.vo.TemplateSelectDownVO;
import org.springblade.modules.admin.vo.TemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Date 10:29 2022/3/11
 * @Description {
 *   任务模板接口
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/maintain/template")
public class MaintainTemplateController{

	@Autowired
	private MaintainTemplateService templateService;

	/**
	 * 任务模板分页查询
	 * @param query
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/page")
	public R<IPage<TemplateVO>> selectTemplatePage(TaskPlanPageQueryDTO pageQueryDTO, Query query){
		return R.data(templateService.selectTemplatePage(pageQueryDTO,Condition.getPage(query)));
	}

	/**
	 * 任务模板详情查询
	 * @param id
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/detail")
	public R<TemplateDetailVO> selectTemplateDetail(Long id){
		return R.data(templateService.selectTemplateDetail(id));
	}

	/**
	 * 任务模板新增
	 * @param template
	 * @return
	 */
	@PostMapping("/save")
	public R<TemplateDetailVO> saveTemplate(@RequestBody MaintainTemplate template){
		return R.data(templateService.saveTemplate(template));
	}

	/**
	 * 任务模板更新
	 * @param id
	 * @param template
	 * @return
	 */
	@PutMapping("/{id}")
	public R<Boolean> updateTemplate(@PathVariable Long id,@RequestBody MaintainTemplate template){
		template.setId(id);
		return R.status(templateService.updateTemplate(template));
	}

	/**
	 * 任务模板删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/remove")
	public R<Boolean> removeTemplate(String ids){
		return R.status(templateService.removeTemplate(Func.toLongList(ids)));
	}

	/**
	 * 任务计划获取模板下拉选择列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/select/down")
	public R<List<TemplateSelectDownVO>> selectDown(String planType,String keyword,String taskPlanIds,String sceneTypeIds){
		return R.data(templateService.getTemplateSelectDown(planType,keyword,Func.toLongList(taskPlanIds),Func.toLongList(sceneTypeIds)));
	}

	/**
	 * 仅满足排期部分根据模板名称可返回重复数据
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/select/down/by/name")
	public R<List<TemplateSelectDownVO>> selectTemplateDownByName(String planType,String keyword,String taskPlanIds){
		return R.data(templateService.selectTemplateDownByName(planType,keyword,Func.toLongList(taskPlanIds)));
	}

	/**
	 * 模板复制
	 * @param template
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/copy")
	public R<Boolean> copy(@RequestBody MaintainTemplate template){
		return R.status(templateService.copy(template.getId()));
	}

	/**
	 * 查询受影响的电梯数
	 * @param templateId 模板id
	 * @param contentIds 工作项id集合
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/elevator/count")
	public R<?> selectElevatorCount(Long templateId,String contentIds){
		return R.data(templateService.selectElevatorCount(templateId,contentIds));
	}

	/**
	 * 根据任务状态获取模板下拉选择列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/select/down/by/plan/status")
	public R<List<TemplateSelectDownVO>> selectDownByPlanStatus(Integer planStatus,String applyElevatorType,String keyword,String taskPlanIds,String sceneTypeIds){
		return R.data(templateService.selectTemplateDownByPlanStatus(planStatus,applyElevatorType,keyword,Func.toLongList(taskPlanIds),Func.toLongList(sceneTypeIds)));
	}

	/**
	 * 根据任务获取模板列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/select/by/plan")
	public R<List<TemplateSelectDownVO>> selectByPlan(Long planId){
		return R.data(templateService.selectByPlan(planId));
	}

	@TenantIgnore
	@GetMapping("/generateWxAppCode")
	@ApiOperation("获取小程序码")
	public R generateWxAppCode(@RequestParam(value = "templateId") Long templateId) {
		String codeUrl = templateService.generateWxAppCode(templateId);
		return R.data(codeUrl);
	}

}
